package cn.rkylin.oms.system.role.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.system.role.dao.RoleDAOImpl;
import cn.rkylin.oms.system.role.domain.WF_ORG_ROLE;
import cn.rkylin.oms.system.role.service.IRoleService;
import cn.rkylin.oms.system.role.vo.RoleVo;
import cn.rkylin.oms.system.unit.domain.WF_ORG_UNIT;
import cn.rkylin.oms.system.user.domain.WF_ORG_USER;

@Controller
@RequestMapping("/role")
public class RoleController extends ApolloController {
    private static final Log logger = LogFactory.getLog(RoleController.class);
    // 常量定义
    private static final String PAGE_SELECT_STATEMENT = "pageSelectShop"; // 分页语句Statement名称
    private static final String GOTO_ROLE_LIST = "/system/organization/role/RoleList"; // 跳转到店铺列表页

    /**
     * 店铺服务层
     */
    @Autowired
    private IRoleService roleService;

    /**
     * 构造函数
     */
    public RoleController() {

    }

    /**
     * 跳转到店铺列表
     */
    @RequestMapping(value = "/gotoRoleList")
    public String gotoRoleList() {
        return GOTO_ROLE_LIST;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 根据查询条件获取角色
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/roleList", method = RequestMethod.GET)
    public Map<String, Object> getRoleList(String quickSearch, RoleVo role, String formJson,
            @RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length)
            throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 前台搜索条件，此处只有一个角色名称
            RoleVo param = new RoleVo();
            if (quickSearch != null) {
                quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
                if (StringUtils.isNotEmpty(quickSearch)) {
                    param.setSearchCondition(quickSearch);
                }
            } else {
                // 高级查询检索条件
                formJson = URLDecoder.decode(formJson, "UTF-8");
                WF_ORG_ROLE roleVo = JSONObject.parseObject(formJson, WF_ORG_ROLE.class);

                if (roleVo != null) {
                    if (StringUtils.isNotEmpty(roleVo.getRoleName())) {
                        param.setRoleName(roleVo.getRoleName());
                    }
                    if (StringUtils.isNotEmpty(roleVo.getIsAdminrole())) {
                        param.setIsAdminrole(roleVo.getIsAdminrole());
                    }
                    if (StringUtils.isNotEmpty(roleVo.getRoleEnable())) {
                        param.setRoleEnable(roleVo.getRoleEnable());
                    }
                }

            }
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser != null && !currentUser.getId().equalsIgnoreCase("adminUser")) {
                WF_ORG_ROLE adminRole = currentUser.getAdminRole();
                if (adminRole != null) {
                    param.setParentRoleId(adminRole.getRoleId());
                    returnMap.put("hasAdminRole", "true");
                } else {
                    returnMap.put("hasAdminRole", "false");
                }
            }
            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;
            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            replaceFieldsMap.put("validateStatus", "validate");
            replaceFieldsMap.put("status", "enable");
            // 排序语句生成
            String orderStatement = getOrderString(RoleDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }
            // 获取分页数据
            PageInfo<RoleVo> roleVOList = roleService.findByWhere(page, length, param);
            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RECORDS_FILTERED, roleVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, roleVOList.getTotal());
            returnMap.put(RETURN_DATA, roleVOList.getList());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 添加角色
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/addRole")
    @SystemControllerLog(module = "角色管理", operation = "添加角色", keyCode = "roleId")
    public Map<String, Object> addRole(@RequestBody RoleVo roleVo) throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        // 具有管理角色的用户只能看自己添加的角色
        CurrentUser securityUser = WebContextFactory.getWebContext().getCurrentUser();
        if (securityUser != null) {
            WF_ORG_ROLE adminRole = securityUser.getAdminRole();
            if (adminRole != null) {
                roleVo.setParentRoleId(adminRole.getRoleId());
            } else {
                // 除非是模拟url提交，否则不会进入
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "必须是管理角色才能使用此功能");
                return returnMap;
            }
        }
        // Over
        try {
            String txtRoleUsers = roleVo.getTxtRoleUsers();
            String txtRoleUnitsValue = roleVo.getTxtRoleUnitsValue();
            List<String> listUnit = new ArrayList<String>();
            if (isRoleUnique(roleVo.getRoleName())) {
                roleVo.setRoleId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
                List<String> listUn = Arrays.asList(txtRoleUnitsValue.split(","));
                if (listUn != null && listUn.size() > 0) {
                    for (int i = 0; i < listUn.size(); i++) {
                        String temp = (String) listUn.get(i);
                        if (temp.indexOf("@") < 0)
                            listUnit.add(temp);
                        else if (temp.indexOf("@UNIT") >= 0)
                            listUnit.add(temp.substring(0, temp.lastIndexOf("@UNIT")));

                    }
                    roleVo.setRoleManageUnitList(listUnit);
                } else {
                    roleVo.setRoleManageUnitList(null);
                }

                if (txtRoleUsers != null && !txtRoleUsers.equals("")) {
                    if (txtRoleUsers.substring(0, 1).equals(","))
                        txtRoleUsers = txtRoleUsers.substring(1);
                    if (txtRoleUsers.substring(txtRoleUsers.length() - 1, txtRoleUsers.length()).equals(","))
                        txtRoleUsers = txtRoleUsers.substring(txtRoleUsers.length() - 1);
                    roleVo.setRoleUsersList(Arrays.asList(txtRoleUsers.split(",")));
                } else {
                    roleVo.setRoleUsersList(null);
                }
                roleVo.setRoleEnable("1");
                roleService.insert(roleVo);
                returnMap.put(JSON_RESULT, SUCCESS);
            } else {
                returnMap.put(JSON_RESULT, FAILED);
                returnMap.put(JSON_MSG, "角色名已存在");
            }
        } catch (Exception ex) {

            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
            System.out.println(ex);
            System.out.println(returnMap);
        }
        returnMap.put("roleId", roleVo.getRoleId());
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/isRoleUnique")
    public boolean isRoleUnique(String roleName) throws Exception {
        roleName = URLDecoder.decode(roleName, "UTF-8");

        boolean result = false;
        RoleVo paramVO = new RoleVo();
        paramVO.setRoleName(roleName);
        PageInfo<RoleVo> roleVOList = roleService.findByWhere(1, 1, paramVO);
        if (roleVOList.getSize() > 0) {
            result = false;
        } else {
            result = true;
        }
        return result;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 删除角色，如果角色已经被使用，则该组织单元不能被删除
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRole")
    @SystemControllerLog(module = "角色管理", operation = "删除角色", keyCode = "roleIds")
    public Map<String, Object> deleteRole(String roleIds) throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            if (roleIds.substring(0, 1).equals(","))
                roleIds = roleIds.substring(1);
            if (roleIds.substring(roleIds.length() - 1, roleIds.length()).equals(","))
                roleIds = roleIds.substring(roleIds.length() - 1);
            roleService.delete(roleIds);

            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("roleIds", roleIds);
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 根据查询条件获取角色
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/getRole", method = RequestMethod.GET)
    public Map<String, Object> getRole(HttpServletRequest request, HttpServletResponse response, String quickSearch, String roleEnable,
            @RequestParam(required = false, defaultValue = "0") int start, @RequestParam(required = false, defaultValue = "10") int length) {
        String roleName = request.getParameter("roleName");
        roleName = (roleName == null || roleName.equals("") || roleName == "%%") ? null : roleName;
        String isAdminRole = request.getParameter("isAdminRole");
        if (isAdminRole != null && !isAdminRole.equals("")) {
            isAdminRole = isAdminRole.equalsIgnoreCase("true") ? "是" : "否";
        } else {
            isAdminRole = null;
        }
        RoleVo roleParam = new RoleVo();
        roleParam.setRoleName(roleName);
        roleParam.setIsAdminrole(isAdminRole);
        // adminUser可以看所有的角色，其它管理员只能看自己的角色
        CurrentUser securityUser = WebContextFactory.getWebContext().getCurrentUser();
        if (securityUser != null && !securityUser.getId().equalsIgnoreCase("adminUser")) {
            WF_ORG_ROLE adminRole = securityUser.getAdminRole();
            if (adminRole != null) {
                roleParam.setParentRoleId(adminRole.getRoleId());
            }
        }
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 前台搜索条件，此处只有一个店铺名称
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");

            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            // 处理快速查询条件
            roleParam.setRoleName(roleName);
            roleParam.setIsAdminrole(isAdminRole);
            if (StringUtils.isNotEmpty(quickSearch) && !quickSearch.equals("undefined")) {
                roleParam.setSearchCondition(quickSearch);
            }

            // 处理转义的字段

            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            replaceFieldsMap.put("validateStatus", "validate");
            replaceFieldsMap.put("status", "enable");

            // 排序语句生成
            String orderStatement = getOrderString(RoleDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                roleParam.setOrderBy(orderStatement);
            }
            if (StringUtils.isNotEmpty(roleEnable)) {
                roleParam.setRoleEnable(roleEnable);
            }

            // 获取分页数据
            PageInfo<RoleVo> roleVOList = roleService.findByWhere(page, length, roleParam);

            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RECORDS_FILTERED, roleVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, roleVOList.getTotal());
            returnMap.put(RETURN_DATA, roleVOList.getList());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 根据查询条件修改角色
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    @SystemControllerLog(module = "角色管理", operation = "修改角色", keyCode = "roleId")
    public Map<String, Object> updateRole(@RequestBody RoleVo roleVo, String txtRoleUsers, String txtRoleUnitsValue) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            txtRoleUnitsValue = roleVo.getTxtRoleUnitsValue();
            txtRoleUsers = roleVo.getTxtRoleUsers();
            List<String> listUnit = new ArrayList<String>();
            if (txtRoleUnitsValue != null && !txtRoleUnitsValue.equals("")) {
                List<String> listUn = Arrays.asList(txtRoleUnitsValue.split(","));
                if (listUn != null && listUn.size() > 0) {
                    for (int i = 0; i < listUn.size(); i++) {
                        String temp = (String) listUn.get(i);
                        if (temp.indexOf("@") < 0)
                            listUnit.add(temp);
                        else if (temp.indexOf("@UNIT") >= 0)
                            listUnit.add(temp.substring(0, temp.lastIndexOf("@UNIT")));

                    }
                    roleVo.setRoleManageUnitList(listUnit);
                }
            } else {
                roleVo.setRoleManageUnitList(null);
            }

            if (txtRoleUsers != null && !txtRoleUsers.equals("")) {
                if (txtRoleUsers.substring(0, 1).equals(","))
                    txtRoleUsers = txtRoleUsers.substring(1);
                if (txtRoleUsers.substring(txtRoleUsers.length() - 1, txtRoleUsers.length()).equals(","))
                    txtRoleUsers = txtRoleUsers.substring(txtRoleUsers.length() - 1);
                roleVo.setRoleUsersList(Arrays.asList(txtRoleUsers.split(",")));
            } else {
                roleVo.setRoleUsersList(null);
            }

            roleService.update(roleVo);
            returnMap.put(JSON_RESULT, SUCCESS);

        } catch (Exception ex) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("roleId", roleVo.getRoleId());
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 获取角色明细
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/getRoleDetail", method = RequestMethod.POST)
    public Map<String, Object> getRoleDetail(String roleId) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            WF_ORG_ROLE roleDetail = roleService.selectRoleDetail(roleId);
            // 拼出角色下的人员~~~~~~~~~~~~~~~~~~
            String roleManageUnitsKEY = "";
            String roleManageUnitsVALUE = "";
            if (roleDetail.getRoleManageUnitList() != null && roleDetail.getRoleManageUnitList().size() > 0) {
                for (int i = 0; i < roleDetail.getRoleManageUnitList().size(); i++) {
                    WF_ORG_UNIT unitVO = (WF_ORG_UNIT) roleDetail.getRoleManageUnitList().get(i);
                    roleManageUnitsKEY += unitVO.getUnitId();
                    roleManageUnitsVALUE += unitVO.getUnitName();
                    // 最后一个字符串后面就不要加“,”了
                    if (i == roleDetail.getRoleManageUnitList().size() - 1) {
                        break;
                    }
                    roleManageUnitsKEY += ",";
                    roleManageUnitsVALUE += ",";
                }
            }
            returnMap.put("roleManageUnitsKEY", roleManageUnitsKEY);
            returnMap.put("roleManageUnitsVALUE", roleManageUnitsVALUE);

            // 拼出角色可以管理的组织~~~~~~~~~~~~~~~~~~
            String roleUsersKEY = "";
            String roleUsersVALUE = "";
            if (roleDetail.getRoleUsersList() != null && roleDetail.getRoleUsersList().size() > 0) {
                for (int i = 0; i < roleDetail.getRoleUsersList().size(); i++) {
                    WF_ORG_USER UserVO = (WF_ORG_USER) roleDetail.getRoleUsersList().get(i);
                    roleUsersKEY += UserVO.getUserId();
                    roleUsersVALUE += UserVO.getUserFullname();
                    // 最后一个字符串后面就不要加“,”了
                    if (i == roleDetail.getRoleUsersList().size() - 1) {
                        break;
                    }
                    roleUsersKEY += ",";
                    roleUsersVALUE += ",";
                }
            }

            returnMap.put("roleUsersKEY", roleUsersKEY);
            returnMap.put("roleUsersVALUE", roleUsersVALUE);

            roleDetail.setRoleManageUnitList(null);
            roleDetail.setRoleUsersList(null);
            returnMap.put("roleDetail", roleDetail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnMap;

    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 获取所有角色，用于角色选择下接列表
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @SuppressWarnings("rawtypes")
    @ResponseBody
    @RequestMapping(value = "/getAllRolesJSON", method = RequestMethod.POST)
    public Map<String, Object> getAllRolesJSON(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            String isAdminRole = request.getParameter("isAdminRole");
            isAdminRole = (isAdminRole == null || isAdminRole.equals("")) ? null : isAdminRole;
            if (isAdminRole != null)
                isAdminRole = isAdminRole.equalsIgnoreCase("true") ? "是" : "否";
            WF_ORG_ROLE roleVO = new WF_ORG_ROLE();
            roleVO.setIsAdminrole(isAdminRole);
            // adminUser可以看所有的角色，其它管理员只能看自己的角色
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser != null && !currentUser.getId().equalsIgnoreCase("adminUser")) {
                WF_ORG_ROLE adminRole = currentUser.getAdminRole();
                if (adminRole != null) {
                    roleVO.setParentRoleId(adminRole.getRoleId());
                }
            }
            // Over
            List roleList = roleService.getAllRoles(roleVO);
            returnMap.put("roleList", roleList);
        } catch (IOException e) {
            returnMap.put("errorMessage", e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 根据role获取角色列表，不分页
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/getRolesInRoleIds", method = RequestMethod.POST)
    public Map<String, Object> getRolesInRoleIds(HttpServletRequest request, HttpServletResponse response) {

        String roleIds = request.getParameter("roleIds");
        Map<String, Object> returnMap = new HashMap<String, Object>();
        List returnRolelist = null;
        try {
            response.setContentType("text/json");
            response.setCharacterEncoding("UTF-8");
            WF_ORG_ROLE roleVO = new WF_ORG_ROLE();
            // 将传入的roleIds格式化成“'sdfdf','3333','4444'”
            String[] idArray = roleIds.split(",");
            List<String> idList = Arrays.asList(idArray);

            roleVO.setRoleIds(idList);
            String roleNames = "";
            if (idArray.length <= 0) {
                returnRolelist = null;
            } else {
                returnRolelist = roleService.getRoleByIDs(roleVO);
                for (int j = 0; j < returnRolelist.size(); j++) {
                    WF_ORG_ROLE role = (WF_ORG_ROLE) returnRolelist.get(j);
                    roleNames += role.getRoleName();
                    if (j < returnRolelist.size() - 1)
                        roleNames += ",";
                }
            }
            returnMap.put("roleNames", roleNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 获取hasAdminRole
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     */
    @ResponseBody
    @RequestMapping(value = "/hasAdminRole", method = RequestMethod.POST)
    public Map<String, Object> hasAdminRole(String userId) throws Exception {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        RoleVo param = new RoleVo();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser != null && !currentUser.getId().equalsIgnoreCase("adminUser")) {
            WF_ORG_ROLE adminRole = currentUser.getAdminRole();
            if (adminRole != null) {
                param.setParentRoleId(adminRole.getRoleId());
                returnMap.put("hasAdminRole", "true");
            } else {
                returnMap.put("hasAdminRole", "false");
            }
        }
        // else{
        // returnMap.put("hasAdminRole","true");
        // }
        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 获取所有角色，用于角色选择下接列表
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @param mapping
     *            - Struts的ActionMapping对象，包含了请求映射的基本信息。
     * @param form
     *            - Struts的ActionForm对象，包含了请求页面要提交的数据(只在配置了FormBean标签时有效)
     * @param request
     *            - jsp请求对象
     * @param response
     *            - jsp答复对象
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/getAllRoles", method = RequestMethod.GET)
    public Map<String, Object> getAllRoles(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            WF_ORG_ROLE roleVO = new WF_ORG_ROLE();
            // adminUser可以看所有的角色，其它管理员只能看自己的角色
            CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
            if (currentUser != null && !currentUser.getId().equalsIgnoreCase("adminUser")) {
                WF_ORG_ROLE adminRole = currentUser.getAdminRole();
                if (adminRole != null) {
                    roleVO.setParentRoleId(adminRole.getRoleId());
                }
            }
            // Over
            List roleList = roleService.getAllRoles(roleVO);
            returnMap.put("roleSelect", roleList);
        } catch (Exception e) {
            request.setAttribute("errorMessage", e.getMessage());
            e.printStackTrace();
        }

        return returnMap;
    }

    /**
     * 方法简要描述信息.
     * <p>
     * 描述: 启用停用
     * </p>
     * <p>
     * 备注: 详见顺序图<br>
     * </p>
     * 
     * @throws 无
     */
    @ResponseBody
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @SystemControllerLog(module = "角色管理", operation = "启用停用", keyCode = "roleId")
    public Map<String, Object> enable(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        String roleEnable = request.getParameter("roleEnable");
        String txtroleid = request.getParameter("roleid");
        try {
            roleService.enableFlg(txtroleid, roleEnable);
            returnMap.put(JSON_RESULT, SUCCESS);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        returnMap.put("roleId", txtroleid);
        return returnMap;
    }

}
